library(tidyverse)
library(tidytext)
library(textdata)
if (!"tweet_id" %in% names(tweets)) {
tweets <- tweets |> mutate(tweet_id = row_number())
}
tweet_words <- tweets |>
select(tweet_id, company_name, text_clean) |>
unnest_tokens(word, text_clean)
lm_lex <- lexicon_loughran() |>
filter(sentiment %in% c("positive", "negative"))
tweet_sent_fin <- tweet_words |>
inner_join(lm_lex, by = "word") |>
mutate(score = if_else(sentiment == "positive", 1, -1))
tweet_scores_fin <- tweet_sent_fin |>
group_by(tweet_id, company_name) |>
summarise(tweet_score = sum(score), .groups = "drop")
company_fin <- tweet_scores_fin |>
group_by(company_name) |>
summarise(net_sent = sum(tweet_score),
n_tweets = n(), .groups = "drop") |>
filter(n_tweets >= 50)